package com.mediacloud.app.newsmodule.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * create by txl
 * at 2018/9/19
 * description :
 */
public class XScrollView extends ScrollView{
    private static final String TAG = "XScrollView";
    private boolean canScroll = false;
    private int scrollDistance = 590;//应该是600 1080*1920
    public XScrollView(Context context) {
        super(context);
    }

    public XScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public XScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private float mLastY;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mLastY = ev.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                if(!canScroll){
                    return false;
                }
                boolean directDown = ev.getRawY() - mLastY>0;
                if(directDown){
                    if(getScrollY() == 0){
                        return false;
                    }
                    return true;
                }else {
                    if(getScrollY() <= scrollDistance){
                        return true;
                    }else {
                        return false;
                    }
                }
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        Log.w(TAG,"onTouchEvent "+"  scrollDistance "+getScrollY()+"   "+ev);
        return super.onTouchEvent(ev);
    }

    public void setCanScroll(boolean canScroll) {
        this.canScroll = canScroll;
    }

    public void setScrollDistance(int scrollDistance) {
        this.scrollDistance = scrollDistance;
    }

}